package com.gitee.wsl.doc.prosemirror.state.command.selection

import com.gitee.wsl.doc.prosemirror.state.Command
import com.gitee.wsl.doc.prosemirror.state.CommandDispatch
import com.gitee.wsl.doc.prosemirror.state.EditorState
import com.gitee.wsl.doc.prosemirror.state.selection.NodeSelection

object SelectParentNode: Command {
    override fun exec(
        state: EditorState,
        dispatch: CommandDispatch?,
    ): Boolean {
        val from = state.selection._from
        val to = state.selection.to
        val same = from.sharedDepth(to)
        if (same == 0) return false
        val pos = from.before(same)
        dispatch?.invoke(state.tr.setSelection(NodeSelection.create(state.doc, pos)))
        return true
    }

}